package com.cris.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class Sale {
	private List<SalesLineItem> lineItems = new ArrayList<SalesLineItem>() ;
	private Date date = new Date();
	private boolean isComplete = false;
	private Payment payment;
	private String orderNo;
	private Money total = new Money();
	public Sale(){
		this.orderNo= UUID.randomUUID().toString();
	}
	public Money getBalance(){
		System.out.println("sale类getbalance获得的payment"+payment.getAmount().getAmount());
		return payment.getAmount().minus(total);	
	}
	public  Money getTotal() {
		Money subtotal = null;
		if (!isComplete){
		for (SalesLineItem lineItem: lineItems) {
			subtotal = lineItem.getSubTotal();
			total.add(subtotal);
		}
		return total;}
		else return total;
	}
	public void becomeComplete() {
		this.isComplete = true;		
	}
	public void makeLineItem(ProductDescription desc, int quantity) {
		lineItems.add(new SalesLineItem(desc,quantity));
		
	}

	public void makePayment(Money cashTendered) {
		if(isComplete){
			payment = new Payment(cashTendered);
			System.out.println(cashTendered);
			System.out.println("sale类makePayment"+payment.getAmount().getAmount());
		}
	}	
	public Date getDate() {
		return date;
	}
	public List<SalesLineItem> getLineItems() {
		return lineItems;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public Payment getPayment(){
		System.out.println(payment.getAmount().getAmount()+"getpayment获得的支付总额");
		return payment;
	}
}
